package com.jn.retrofit.views;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.jn.retrofit.R;
import com.jn.retrofit.entity.HotSearchResponse;
import com.jn.retrofit.service.ApiService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    private static final String BASE_URL = "https://api.oioweb.cn/api/";
    private ApiService apiService;

    private EditText editText;
    private Button buttonGet, buttonPost, buttonDict;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.edit);
        buttonGet = findViewById(R.id.button_get);
        buttonPost = findViewById(R.id.button_post);
        buttonDict = findViewById(R.id.button_dict);

        // 创建 Retrofit 实例
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        apiService = retrofit.create(ApiService.class);

        buttonGet.setOnClickListener(view -> {
            String platform = editText.getText().toString().trim();
            if (!platform.isEmpty()) {
                fetchHotSearch(platform);
            } else {
                Toast.makeText(MainActivity.this, "请输入平台名", Toast.LENGTH_SHORT).show();
            }
        });

        buttonDict.setOnClickListener(view -> {
            String queryText = editText.getText().toString().trim();
            if (!queryText.isEmpty()) {
                Intent intent = new Intent(MainActivity.this, DataDisplayActivity.class);
                intent.putExtra("inputText", queryText);
                startActivity(intent);
            } else {
                Toast.makeText(MainActivity.this, "请输入汉字", Toast.LENGTH_SHORT).show();
            }
        });

        buttonPost.setOnClickListener(view -> {
            Intent intent = new Intent(MainActivity.this, TranslationActivity.class);
            startActivity(intent);
        });
    }

    private void fetchHotSearch(String platform) {
        apiService.fetchHotSearchBoard(platform).enqueue(new Callback<HotSearchResponse>() {
            @Override
            public void onResponse(Call<HotSearchResponse> call, Response<HotSearchResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    List<HotSearchResponse.HotSearchItem> hotSearchItems = response.body().getResult();
                    // 处理并跳转到 ResultActivity
                    Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                    intent.putExtra("hotSearchItems", new Gson().toJson(hotSearchItems));
                    startActivity(intent);
                } else {
                    Toast.makeText(MainActivity.this, "获取热搜数据失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<HotSearchResponse> call, Throwable t) {
                Toast.makeText(MainActivity.this, "请求失败: " + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
